package com.dongdongshop.operator.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.front.pojo.TbContentCategory;
import com.dongdongshop.front.service.ContentCategoryService;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contentCategory")
public class ContentCategoryController {

    private final Logger logger = LoggerFactory.getLogger(ContentCategoryController.class);

    @Reference(check = false)
    private ContentCategoryService contentCategoryService;

    //跳转到广告类型管理
    @RequestMapping("toContentCategory")
    public String toContentCategory(){
        return "admin/content_category";
    }

    //获取广告类型
    @ResponseBody
    @RequestMapping("getContentCategoryOrName")
    public Result getContentCategoryOrName(String name){
        try {
            List<TbContentCategory> contentCategoryList = contentCategoryService.getContentCategoryOrName(name);
            return Result.success(ResultEnum.SUCCESS,contentCategoryList);
        }catch (Exception e){
            logger.info(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //添加
    @ResponseBody
    @RequestMapping("addContentCategory")
    public Result addContentCategory(String name){
        try {
            contentCategoryService.addContentCategory(name);
            return Result.perfect(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.info(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //回显
    @ResponseBody
    @RequestMapping("getContentCategoryById")
    public Result getContentCategoryById(Long id){
        try {
            TbContentCategory contentCategory = contentCategoryService.getContentCategoryById(id);
            return Result.success(ResultEnum.SUCCESS,contentCategory);
        }catch (Exception e){
            logger.info(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //修改
    @ResponseBody
    @RequestMapping("updateContentCategory")
    public Result updateContentCategory(TbContentCategory contentCategory){
        try {
            contentCategoryService.updateContentCategory(contentCategory);
            return Result.perfect(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.info(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //批量删除
    @ResponseBody
    @RequestMapping("deleteByIds")
    public Result deleteByIds(@RequestParam(value = "ids[]")Long[] ids){
        try {
            contentCategoryService.deleteByIds(ids);
            return Result.perfect(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.info(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
}
